<?php

require_once('class_form_element.php');

class FormElementFactory
{
  public function __construct() { }

  public function createInstance($data)
  {
    list($name, $type, $null, $key, $default, $extra) = $data;
    if ($extra == 'auto_increment') {

      $element = new HiddenElement($name);
      $element->setRequired(false);

    } elseif (strpos($type, 'enum(') !== false) {

      /* Break the type info into an array of options for a select box */
      $options = substr($type, 6, -2);
      $options = explode("','", $options);
      $element = new SelectElement($name, NULL, $options);

    } elseif ($type == 'tinyint(1)') {

      /* For boolean values, create a checkbox */
      $element = new CheckboxElement($name, NULL, '1');

    } elseif (strpos($type, 'int') !== false) {

      /* For integer fields create a textbox but add a numeric rule */
      $element = new TextElement($name);
      $element->addValidationRule(new NumericValidationRule());

    } elseif ($type == 'date') {

      $element = new DateElement($name);

    } elseif ($type == 'time') {

      $element = new TimeElement($name);

    } elseif (strpos($type, 'set(') !== false) {

      $options = substr($type, 5, -2);
      $options = explode("','", $options);
      $element = new CheckboxesElement($name, NULL, $options);

    } else {

      $element = new TextElement($name);

    }
    return $element;
  }
}
?>